package com.wang.web3;

import android.os.Bundle;
import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;

import com.google.gson.Gson;
import com.wang.web3.adapter.TransactionAdapter;
import com.wang.web3.base.Constant;
import com.wang.web3.bean.TianWallet;
import com.wang.web3.bean.TransactionBean;
import com.wang.web3.data.service.ICallBackListener;
import com.wang.web3.data.service.impl.ApiServiceImpl;
import com.wang.web3.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

public class TransactionActivity extends Activity implements TransactionAdapter.PersonHolderListener{

    RecyclerView recyclerView;
    TransactionAdapter transactionAdapter;
    List<TransactionBean.DataBean> list;
    Gson gson = new Gson();
    TransactionBean transactionBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction);
        recyclerView = findViewById(R.id.mTranRv);
        findViewById(R.id.mLeftIv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        list = new ArrayList<>();
        transactionAdapter = new TransactionAdapter(list,this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(transactionAdapter);

        initData();

    }

    private void initData() {
        String lockWallet = (String) SPUtils.get(this, Constant.WALLETLOCKADDRESS,"");
        if (lockWallet.equals("")){
            return;
        }
        TianWallet tianWallet = gson.fromJson(lockWallet,TianWallet.class);
        ApiServiceImpl.getInstence().transactionList(tianWallet.getAdress(), new ICallBackListener() {
            @Override
            public void onSuccess(String string) {
                transactionBean = gson.fromJson(string,TransactionBean.class);
                list.addAll(transactionBean.getData());
                transactionAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFaild(String string) {

            }
        });
    }

    @Override
    public void onPersonItemClicked(View v, MotionEvent event, int position) {

    }
}
